package com.courseSchedulingSystem.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Base64;
import java.util.Calendar;
import java.util.Map;

public class AdminJwtUtils {
    //生成签名
    private static final String SIGN = "djqwiue";
    /*
    * 生成token  header.payload.sign
    **/

    public static String getToken(Map<String,String> data){
        Calendar instance  = Calendar.getInstance();
          instance.add(Calendar.DATE,1);//默认过期时间为1天

        //创建jwt builder
        JWTCreator.Builder builder = JWT.create();
        //payload
        data.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        String token = builder.withExpiresAt(instance.getTime())//指定过期时间
                .sign(Algorithm.HMAC256(SIGN));//指定签名
        return token;
    }


    /*
    *  验证token合法性
    **/
    public static DecodedJWT  verify(String token){
        return  JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);

    }
}
